package com.shihang.blog.controller;

import com.shihang.blog.bean.Article;
import com.shihang.blog.config.PageConfig;
import com.shihang.blog.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ArticleController {

    @Autowired
    ArticleMapper articleMapper;

    @RequestMapping("/articles")
    @ResponseBody
    public List<Article> queryArticles(){
        return articleMapper.queryArticles();
    }
    @RequestMapping("/queryArticlesByParams")
    @ResponseBody
    public List<Article> queryArticlesByParams(@RequestParam Map<String,Object> map){
        if(!map.containsKey("index")){
            map.put("index","1");
        }
        if(!map.containsKey("pageSize")){
            map.put("pageSize","20");
        }
        List<Article> articles = articleMapper.queryArticlesByParams(map);
        System.out.println(articles.size());
        return articles;
    }

    @RequestMapping("/articleList")
    @ResponseBody
    public List<Article> articleList(int page,int pageSize){
        PageConfig pageConfig = new PageConfig();
        pageConfig.setPage(page);
        pageConfig.setPageSize(pageSize);
        List<Article> articles = articleMapper.queryArticleByPage(pageConfig);
        return articles;
    }


    @RequestMapping("/article")
    @ResponseBody
    public Article getArticleDetail(int id){
        Article article = articleMapper.findArticleById(id);
        return article;
    }



}
